// 游戏板尺寸
const BOARD_ROWS = 20;
const BOARD_COLS = 20;

// 获取游戏板元素
const gameBoard = document.getElementById('game-board');

// 初始化游戏板数组
const board = [];
for (let i = 0; i < BOARD_ROWS; i++) {
    board[i] = new Array(BOARD_COLS).fill(0);
}

// 吃豆人初始位置
let pacmanX = 10;
let pacmanY = 10;

// 初始化硬币
function initCoins() {
    for (let i = 0; i < BOARD_ROWS; i++) {
        for (let j = 0; j < BOARD_COLS; j++) {
            if (i!== pacmanY || j!== pacmanX) {
                board[i][j] = 1; // 1 表示硬币
            }
        }
    }
}

// 更新游戏板显示
function updateBoard() {
    gameBoard.innerHTML = '';
    for (let i = 0; i < BOARD_ROWS; i++) {
        for (let j = 0; j < BOARD_COLS; j++) {
            const cell = document.createElement('div');
            cell.classList.add('cell');
            if (i === pacmanY && j === pacmanX) {
                cell.classList.add('pacman');
            } else if (board[i][j] === 1) {
                cell.classList.add('coin');
            }
            gameBoard.appendChild(cell);
        }
    }
}

// 移动吃豆人
function movePacman(dx, dy) {
    const newX = pacmanX + dx;
    const newY = pacmanY + dy;
    if (newX >= 0 && newX < BOARD_COLS && newY >= 0 && newY < BOARD_ROWS) {
        if (board[newY][newX] === 1) {
            board[newY][newX] = 0; // 吃掉硬币
        }
        pacmanX = newX;
        pacmanY = newY;
        updateBoard();
    }
}

// 键盘事件处理
document.addEventListener('keydown', function (event) {
    switch (event.key) {
        case 'ArrowLeft':
            movePacman(-1, 0);
            break;
        case 'ArrowRight':
            movePacman(1, 0);
            break;
        case 'ArrowUp':
            movePacman(0, -1);
            break;
        case 'ArrowDown':
            movePacman(0, 1);
            break;
    }
});

// 初始化游戏
initCoins();
updateBoard();